// 当前页码和每页显示数量
let currentPage = 1;
let pageSize = 12;
let total = 0;

// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', () => {
    loadGoodsList();
});

// 加载商品列表
async function loadGoodsList() {
    try {
        const response = await axios.get('/goods/list', {
            params: {
                current: currentPage,
                pagesize: pageSize
            }
        });

        if (response.data.code === 1) {
            const { list, total: totalItems } = response.data;
            total = totalItems;
            renderGoodsList(list);
            updatePagination(totalItems);
        }
    } catch (error) {
        console.error('加载商品列表失败:', error);
    }
}

// 渲染商品列表
function renderGoodsList(goods) {
    const goodsList = document.getElementById('goodsList');
    goodsList.innerHTML = goods.map(item => {
        const currentPrice = parseFloat(item.current_price).toFixed(2);
        const originalPrice = item.price ? parseFloat(item.price).toFixed(2) : null;
        
        return `
            <div class="goods-item" onclick="location.href='goods-detail.html?id=${item.goods_id}'">
                ${item.is_hot ? '<span class="hot-tag">热销</span>' : ''}
                ${item.sale_type === 'today' ? '<span class="today-tag">今日送10元券</span>' : ''}
                <img src="${item.img_big_logo}" alt="${item.title}" class="goods-img">
                <h3 class="goods-title">${item.title}</h3>
                <div class="price-container">
                    <span class="current-price">¥${currentPrice}</span>
                    ${originalPrice ? `<span class="original-price">¥${originalPrice}</span>` : ''}
                </div>
            </div>
        `;
    }).join('');
}

// 更新分页信息
function updatePagination(total) {
    const totalPages = Math.ceil(total / pageSize);
    document.getElementById('currentPage').textContent = currentPage;
    document.getElementById('totalPages').textContent = totalPages;
}

// 切换页码
function changePage(type) {
    const totalPages = Math.ceil(total / pageSize);
    
    switch(type) {
        case 'first':
            currentPage = 1;
            break;
        case 'prev':
            currentPage = Math.max(1, currentPage - 1);
            break;
        case 'next':
            currentPage = Math.min(totalPages, currentPage + 1);
            break;
        case 'last':
            currentPage = totalPages;
            break;
    }
    
    loadGoodsList();
}

// 改变每页显示数量
function changePageSize() {
    const select = document.querySelector('.page-size-select');
    pageSize = parseInt(select.value);
    currentPage = 1;
    loadGoodsList();
}

// 跳转到指定页面
function jumpToPage() {
    const jumpInput = document.getElementById('jumpPage');
    const pageNum = parseInt(jumpInput.value);
    const totalPages = Math.ceil(total / pageSize);
    
    if (pageNum && pageNum > 0 && pageNum <= totalPages) {
        currentPage = pageNum;
        loadGoodsList();
    } else {
        alert('请输入有效的页码！');
    }
} 